package com.trust.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trust.agri.response.ResponseResult;
import com.trust.agri.sign.validate.SaveValidate;
import com.trust.agri.sign.validate.UpdateValidate;
import com.trust.annotation.SYQLog;
import com.trust.entity.DTO.FarmSeedDTO;
import com.trust.entity.Param.FarmSeedListParam;
import com.trust.entity.FarmSeed;
import com.trust.entity.VO.FarmSeedListVO;
import com.trust.entity.VO.FarmSeedVO;
import com.trust.entity.result.DropDownChildResult;
import com.trust.entity.result.DropDownResult;
import com.trust.service.BreedService;
import com.trust.service.FarmSeedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/api/farm/seed")
@Api(value = "农事作业 - 种植任务 育苗", tags = "农事作业 - 种植任务 育苗")
public class FarmSeedController {

    @Resource
    private FarmSeedService service;

    @Resource
    private BreedService breedService;



    /**
     * 列表查询
     */
    @PostMapping("/list")
    @ApiOperation("育苗列表")
    public ResponseResult<Page<FarmSeedListVO>> list(@RequestBody FarmSeedListParam param){
        Page<FarmSeedListVO> page = service.list(param);
        return ResponseResult.ok(page);
    }

    /**
     * 新增育苗
     */
    @PostMapping("/save")
//    @ApiOperation("新增育苗")
//    @SYQLog(type = "新增育苗", name = "种植任务 育苗")
    public ResponseResult<FarmSeed> save(@Validated(SaveValidate.class) @RequestBody FarmSeedDTO param){
        FarmSeed farmSeed = service.saveAndUpdate(param);
        return ResponseResult.ok(farmSeed);
    }

    @GetMapping("/breed")
    @ApiOperation("种植 种源批号下拉框")
    public ResponseResult<List<DropDownResult>> breed(@RequestParam Integer medicineId){
        List<DropDownResult> res = service.breed(medicineId);
        return ResponseResult.ok(res);
    }

    @GetMapping("/breed/tree/all")
    @ApiOperation("种植 种源批号下拉框(树结构)")
    public ResponseResult<List<DropDownChildResult>> breed(){
        List<DropDownResult> breed = breedService.dropDown();
        List<DropDownChildResult> dropDownChildResults = BeanUtil.copyToList(breed, DropDownChildResult.class);
        for (DropDownChildResult dropDownChildResult : dropDownChildResults) {
            List<DropDownResult> res = service.breed(dropDownChildResult.getId());
            if(CollUtil.isNotEmpty(res)){
                dropDownChildResult.setChildren(BeanUtil.copyToList(res, DropDownChildResult.class));
            }
        }
        return ResponseResult.ok(dropDownChildResults);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("育苗详情")
    public ResponseResult<FarmSeedVO> info(@PathVariable Integer id){
        FarmSeedVO result = service.info(id);
        return ResponseResult.ok(result);
    }

    @PostMapping("/update")
    @ApiOperation("更新育苗")
    @SYQLog(type = "更新育苗", name = "种植任务 育苗")
    public ResponseResult<FarmSeed> update(@Validated(UpdateValidate.class) @RequestBody FarmSeedDTO param){
        FarmSeed farmSeed = service.saveAndUpdate(param);
        return ResponseResult.ok(farmSeed);
    }

    @PostMapping("/remove")
    @ApiOperation("删除育苗")
    @SYQLog(type = "删除育苗", name = "种植任务 育苗", tagGet = SYQLog.PARAM)
    public ResponseResult<String> remove(@RequestBody List<Integer> ids){
        service.removeByIds(ids);
        return ResponseResult.ok();
    }

}
